﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;

namespace OurFAS.MySystem
{
    public partial class FrmAccount : Form
    {
        #region 成员变量
        private string _sysAccount = "admin";
        #endregion
        
        #region 构造函数
        public FrmAccount()
        {
            InitializeComponent();
        }
        #endregion

        #region 控件函数
        private void FrmAccount_Load(object sender, EventArgs e)
        {
            RefreshAccountList();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmAccountDetail window = new FrmAccountDetail(Data.EnumHelper.EnumObjctOperateType.New, null);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                RefreshAccountList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Edit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要删除的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            Base_Account record = row.DataBoundItem as Base_Account;
            
            if (record.UserCode.Trim() == _sysAccount)
            {
                MessageBox.Show("系统管理员不能删除！");
                return;
            }
            
            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
           
            OurFasDBEntities entity = new OurFasDBEntities();
            Base_Account delRecord 
                = entity.Base_Account.Where(c => c.UserCode.Trim() == record.UserCode).FirstOrDefault();
         
            try
            {
                if (delRecord != null)
                {
                    entity.Base_Account.DeleteObject(delRecord);
                    entity.SaveChanges();
                    MessageBox.Show("删除用户成功!");
                    RefreshAccountList();
                }
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(this, "删除项出错，错误：" + exc.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 私有函数
        private void RefreshAccountList()
        {
            dgv.DataSource = null;
            OurFasDBEntities entity = new OurFasDBEntities();
            if (entity.Base_Account.Count() > 0)
            {
                dgv.DataSource = entity.Base_Account;
            }
        }

        private void Edit()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            Base_Account record = row.DataBoundItem as Base_Account;
            FrmAccountDetail window = new FrmAccountDetail(Data.EnumHelper.EnumObjctOperateType.Edit, record);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshAccountList();
            }
        }
        #endregion
    }
}
